﻿using IDEAMS.common;
using IDEAMS.core;
using IDEAMS.model;
using Microsoft.Win32;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;


namespace IDEAMS.view.detection
{
    /// <summary>
    /// RoasterRecordDlg.xaml 的交互逻辑
    /// </summary>
    public partial class RoasterRecordDlg : Window
    {
        private RoasterRecordDlgViewModel windowViewModel;
        public RoasterRecordDlg()
        {
            InitializeComponent();

            windowViewModel = new RoasterRecordDlgViewModel();
            this.DataContext = windowViewModel;

            this.btnSearch.Click += new RoutedEventHandler(Search_Click);
            this.btnCreateReport.Click += new RoutedEventHandler(CreateReport_Click);
        }

        private void Search_Click(object sender, RoutedEventArgs e)
        {
            windowViewModel.SearchRoasterRecords();
        }

        private void CreateReport_Click(object sender, RoutedEventArgs e)
        {
            List<RoasterRecordViewModel> selectedItems = dgRoasterRecord.SelectedItems.Cast<RoasterRecordViewModel>().ToList();
            if (selectedItems == null || selectedItems.Count != 1) return;

            DeviceInfo deviceInfo = new DeviceInfo();
            deviceInfo.Load(selectedItems[0].serialNo);

            SaveFileDialog saveFileDialog = new SaveFileDialog();

            // 设置文件过滤器
            saveFileDialog.Filter = "Word 文档 (*.docx)|*.docx";

            // 设置默认文件名
            saveFileDialog.FileName = deviceInfo.deviceParam.type + "-出厂检验报告.docx";

            // 设置对话框标题
            saveFileDialog.Title = "生成检验报告";

            // 显示保存文件对话框
            bool? result = saveFileDialog.ShowDialog();

            // 检查用户是否点击了保存按钮
            if (result == true)
            {
                // 获取用户选择的文件路径
                string filePath = saveFileDialog.FileName;

                CreateReport.Create(filePath, new DeviceInfo(deviceInfo));
            }
        }
    }

    public class RoasterRecordDlgViewModel : ViewModelBase
    {
        private RoasterRecordCollection roasterRecordCollection;
        public RoasterRecordDlgViewModel()
        {
            roasterRecordCollection = TW_App.roasterRecordCollection.Clone();

            roasterRecords = new ObservableCollection<RoasterRecordViewModel>();

            roasterRecordCollection.ForEach(x =>
            {
                RoasterRecordViewModel roasterRecordViewModel = new RoasterRecordViewModel(x);
                roasterRecords.Add(roasterRecordViewModel);
                return false;
            });

            var sortList = roasterRecords.OrderByDescending(item => item.dateTime).ToList();

            roasterRecords.Clear();
            foreach (var item in sortList)
            {
                roasterRecords.Add(item);
            }
        }

        public void SearchRoasterRecords()
        {
            roasterRecords.Clear();
            if (string.IsNullOrEmpty(inputSerialNo))
            {
                roasterRecordCollection.ForEach(x =>
                {
                    RoasterRecordViewModel roasterRecordViewModel = new RoasterRecordViewModel(x);
                    roasterRecords.Add(roasterRecordViewModel);
                    return false;
                });
            }
            else
            {
                RoasterRecord roasterRecord = roasterRecordCollection.Find(inputSerialNo);
                if(roasterRecord != null)
                {
                    roasterRecords.Add(new RoasterRecordViewModel(roasterRecord));
                }
            }

        }

        private ObservableCollection<RoasterRecordViewModel> m_roasterRecords;
        public ObservableCollection<RoasterRecordViewModel> roasterRecords
        {
            get { return m_roasterRecords; }
            set
            {
                m_roasterRecords = value;
                NotifyPropertyChanged();
            }
        }

        private string m_inputSerialNo;
        public string inputSerialNo
        {
            get { return m_inputSerialNo; }
            set
            {
                m_inputSerialNo = value;
                NotifyPropertyChanged();
            }
        }
    }
}
